package com.oocl.dao.impl;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.UUID;

import com.oocl.dao.OrderDao;
import com.oocl.pojo.Orders;
import com.oocl.util.DBUtil;

public class OrderDaoImpl implements OrderDao{

	@Override
	public int addOrder(Orders order) {
		Connection connection  = null;
		connection = DBUtil.getConnection();
		PreparedStatement ps = null;
		String sql = "insert into orders(id,u_id,b_id,count,createTime) values(?,?,?,?,?)";
		int n=0;
		try {
			ps = connection.prepareStatement(sql);
			ps.setString(1, UUID.randomUUID().toString());
			ps.setString(2, order.getUserId());
			ps.setString(3, order.getBookId());
			ps.setInt(4, order.getCount());
			ps.setDate(5, new Date(new java.util.Date().getTime()));
			n = ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			DBUtil.release(null, ps, connection);
		}
		return n;
	}

	@Override
	public int addOrders(List<Orders> orders) {
		Connection connection  = null;
		connection = DBUtil.getConnection();
		PreparedStatement ps = null;
		String sql = "insert into orders(id,u_id,b_id,count,createTime) values(?,?,?,?,?)";
		int n=0;
		try {
			ps = connection.prepareStatement(sql);
			for (Orders order : orders) {
				ps.setString(1, UUID.randomUUID().toString());
				ps.setString(2, order.getUserId());
				ps.setString(3, order.getBookId());
				ps.setInt(4, order.getCount());
				ps.setDate(5, new Date(new java.util.Date().getTime()));
				ps.addBatch();
			}
			int[] ms = ps.executeBatch();
			n = ms.length;
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			DBUtil.release(null, ps, connection);
		}
		return n;
	}

}
